package yuan.common;

import com.alibaba.fastjson.JSONObject;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.ImmutableTag;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class MyGauge {
    static List<Tag> init(){
        ArrayList<Tag> list = new ArrayList(){};
        list.add(new ImmutableTag("vip", "2"));
        return list;
    }

    static AtomicInteger atomicInteger = new AtomicInteger(0);

    static Gauge guage1 = Gauge.builder("a_user_gauge", atomicInteger, AtomicInteger::get)
            .tag("vip", "1")
            .description("vip 1 user num")
            .register(Metrics.globalRegistry);


    static AtomicInteger guage2 =  Metrics.gauge("a_user_gauge", init(), atomicInteger);

    public static String handleMetrics() {

//        while (true){
            if (System.currentTimeMillis() % 2 == 0){
                guage2.addAndGet(100);
                System.out.println("ADD + " + guage1.measure() + " : " + guage2);
            }else {
                int val = guage2.addAndGet(-100);
                if (val < 0){
                    guage2.set(1);
                }
                System.out.println("DECR - " + guage1.measure() + " : " + guage2);
            }
//        }
        return JSONObject.toJSONString(guage1.measure());

    }

}
